package io.xxx.walle.task;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.springframework.lang.NonNull;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Component
public class PushTaskJob extends QuartzJobBean {

    @Resource
    private TaskRepository taskRepository;

    @Resource
    private TaskService taskService;

    @Override
    protected void executeInternal(@NonNull JobExecutionContext context) {
        List<Task> tasks = taskRepository.findAllByStartTimeGreaterThanEqual(LocalDateTime.now());
        tasks.parallelStream()
                .forEach(task -> {
                    log.info("任务开始执行[taskId:{},taskName:{}]", task.getId(), task.getName());
                    System.out.println(task);
                    taskService.push(task);
                });
    }
}
